import { Request } from '../../wxParse/request.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: getApp().globalData.statusBarHeight,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.activitId = options.id; //活动详情列表id
    // 登录
    wx.login({
      success: function(res){
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          wx.request({
            url: 'https://weizhi.darenjian.com/api/pinduo/chou_award/login/login',
            method: 'POST',
            data: {
              code: res.code,
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'cookie': 'PHPSESSID=' + 1
            },
            success: function(res){
              console.log(res.data.loginCode)
              if(res.data.loginCode == 1){
                app.globalData.loginStatus = true;   //授权登录
                app.globalData.openid = res.data.openid;
                app.globalData.userid = res.data.userId;       //用户id
                app.globalData.userName = res.data.userName;   //用户名
              }else{
                app.globalData.loginStatus = false;  //未授权登录
                app.globalData.openid = res.data.openid; 
              }
              
              this.GotoActivityInfo();  //加载活动详情信息
              
              app.globalData.status = res.data.status;       //用户登录状态
              this.setData({
                loginStatus: app.globalData.loginStatus
              });
              console.log(app)
            }.bind(this),
          });
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }.bind(this),
    });
    
    // console.log(options.activitId);
    
  },
  /**
     * 用户授权登陆
     */
    bindGetUserInfo(e) {
      console.log(e);
      //取消授权 执行不了此API方法
      wx.getUserInfo({
          success: (res) => {
              console.log(res);
              app.globalData.userInfo = res.userInfo;
              
              //注册用户
              if (app.globalData.loginStatus != true) {
                //还没有授权登陆
                  new Request('Login/registerUser').request(
                      () => {
                          return {
                              openid: app.globalData.openid, //用户openid
                              userName: res.userInfo.nickName, //用户名
                              userAvatar: res.userInfo.avatarUrl, //用户头像
                              userCity: res.userInfo.city, //用户城市
                          };
                      },
                      (res) => {
                          console.log(res.data.msg);
                          console.log(app.globalData);
                          app.globalData.loginStatus = true; //用户授权登录 修改状态
                          this.setData({loginStatus: true,});
                      }
                  );
              } else {
                //已经授权登陆了
                this.setData({
                  loginStatus: true,
                });
              }
          },
      });
      if (typeof e.detail.userInfo == 'undefined' || e.detail.userInfo == '') {
          //用点击了授权取消
          wx.showToast({
              title: '取消授权',
              icon: 'none',
              duration: 2000,
          });
      } else {
          console.log('授权了');
      }
  },
   /**
   * 
   * @param {参与此活动 | 报名} param 
   */
  _buttonTakeActivity:function(param){
    console.log(param.currentTarget.dataset.take);
    if(param.currentTarget.dataset.take == -4){
      wx.showToast({
        title: '已报名了',
        icon: 'none',
        duration: 2000
      });
    }else if(param.currentTarget.dataset.take == -5){
      wx.showToast({
        title: '已开奖',
        icon: 'none',
        duration: 2000
      });
    }else if(param.currentTarget.dataset.take == -6){
      //判断暗号 输入正确的暗号才可以参与抽奖  判断是否有暗号
      if(!this.data.ActivityInfo.activtiyTackCode != ''){
        wx.requestSubscribeMessage({
          tmplIds: ['klQFw9podMSiQaC-ZFmKQx5LUtUfyy3ICPCyka7SQOc','cLL0NK4kh5SkkWREW_brM6TZY6zDbbXSz3Xb4G-Bpj0'],
          complete: ()=>{
            //用户 点击参与活动报名
            new Request('Chou/TakeActivity').request(
              () => {
                return {
                  openid: app.globalData.openid,    //用户openid
                  ActivityId: this.activitId,     //活动详情列表id
                }
              },
              (res) => {
                if(res.data.data == 1){
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                  });
                  setTimeout(function(){
                    //两秒后刷新 按钮 活动页面内容
                    this.shuaxing();
                  }.bind(this),2001);
                }else{
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                  });
                }
            });
          }
        });
      }else{
        
        //有暗号的输入
        if(typeof(this.AnHaoText) != 'undefined' && this.AnHaoText != ''){
          //正确的暗号
          if(this.data.ActivityInfo.activtiyTackCode == this.AnHaoText){
            wx.requestSubscribeMessage({
              tmplIds: ['klQFw9podMSiQaC-ZFmKQx5LUtUfyy3ICPCyka7SQOc','cLL0NK4kh5SkkWREW_brM6TZY6zDbbXSz3Xb4G-Bpj0'],
              complete: ()=>{
                //用户 点击参与活动报名
                new Request('Chou/TakeActivity').request(
                  () => {
                    return {
                      openid: app.globalData.openid,    //用户openid
                      ActivityId: this.activitId,     //活动详情列表id
                    }
                  },
                  (res) => {
                    if(res.data.data == 1){
                      wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2000
                      });
                      //关闭暗号显示输入框
                      this.setData({
                        ShowCodeBox: false,
                      });
                      setTimeout(function(){
                        //两秒后刷新 按钮
                        this.shuaxing();
                      }.bind(this),2001);
                    }else{
                      wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2000
                      });
                    }
                  });
                }
            });
          }else{
            wx.showToast({
              title: '暗号错误',
              icon: 'none',
              duration: 2000
            });
          }
        }else{
          console.log(this.AnHaoText);
          //开启暗号显示输入框
          this.setData({
            ShowCodeBox: true,
          });
          if(this.click >=2){
            wx.showToast({
              title: '请输入暗号',
              icon: 'none',
              duration: 2000
            });
          }
          this.click++;
        }
      }
    }else if(param.currentTarget.dataset.take == -7){
      wx.showToast({
        title: '将自己的活动分享给好友吧',
        icon: 'none',
        duration: 2000
      });
    }
    
  },
  /**
   * 刷新 参与报名的 按钮
   */
  shuaxing:function(){
    //获取用户是否参与此抽奖活动
    new Request('Chou/TakeActivity').request(
      () => {
        return {
          openid: app.globalData.openid,    //用户openid
          ActivityId: this.activitId,     //活动详情列表id
          SelectUserTake: 1,                //开启查询结果
        }
      },
      (res) => {
         //获取用户参与状态  -4是代表参与过了
         let UserTakeActivityStatus = res.data.data;
         if(UserTakeActivityStatus == -6){
           var StyleAct = 'background-color: #E95750;border: 1rpx solid #FF0000;';
           var StyleTet = '参与抽奖';
         }else if(UserTakeActivityStatus == -5){
           var StyleAct = 'background-color: #999999;border: 1rpx solid #777777;';
           var StyleTet = '已开奖';
         }else if(UserTakeActivityStatus == -4){
           var StyleAct = 'background-color: #66CC00;border: 1rpx solid #669900;';
           var StyleTet = '已报名';
         }else if(UserTakeActivityStatus == -7){
           var StyleAct = 'background-color: #E95750;border: 1rpx solid #FF0000;';
           var StyleTet = '分享活动';
         }else if(UserTakeActivityStatus == -8){
           var StyleAct = 'background-color: #999999;border: 1rpx solid #777777;';
           var StyleTet = '已结束';
         }
         this.setData({
           ShowStatusButton: StyleAct,
           StyleText:StyleTet,
           UserTakeActivityStatus:res.data.data
         });
        
      });
  },
  // onLond 加载详情信息
  GotoActivityInfo:function(param){
    //获取活动详情信息接口
    new Request('Activity/GetActivityDetail').request(
      () => {
        return {
          openid: app.globalData.openid,    //用户openid
          ActivityId: this.activitId     //活动详情列表id
        }
      },
      (res) => {
        //活动内容信息
        this.setData({
          ActivityInfo: res.data.data,
        });
       
      });

      //获取用户是否参与此抽奖活动
      new Request('Chou/TakeActivity').request(
        () => {
          return {
            openid: app.globalData.openid,    //用户openid
            ActivityId: this.activitId,     //活动详情列表id
            SelectUserTake: 1,                //开启查询结果
          }
        },
        (res) => {
          //获取用户参与状态  -4是代表参与过了
          let UserTakeActivityStatus = res.data.data;
          if(UserTakeActivityStatus == -6){
            var StyleAct = 'background-color: #E95750;border: 1rpx solid #FF0000;';
            var StyleTet = '参与抽奖';
          }else if(UserTakeActivityStatus == -5){
            var StyleAct = 'background-color: #999999;border: 1rpx solid #777777;';
            var StyleTet = '已开奖';
          }else if(UserTakeActivityStatus == -4){
            var StyleAct = 'background-color: #66CC00;border: 1rpx solid #669900;';
            var StyleTet = '已报名';
          }else if(UserTakeActivityStatus == -7){
            var StyleAct = 'background-color: #E95750;border: 1rpx solid #FF0000;';
            var StyleTet = '分享活动';
          }else if(UserTakeActivityStatus == -8){
            var StyleAct = 'background-color: #999999;border: 1rpx solid #777777;';
            var StyleTet = '已结束';
          }
          this.setData({
            ShowStatusButton: StyleAct,
            StyleText:StyleTet,
            UserTakeActivityStatus:res.data.data
          });
          
      });
  },
  //隐藏输入暗号透明层
  hideShowCodeBox:function(){
    this.setData({
      ShowCodeBox:false,
    });
  },
  /**
   * 
   * @param {获取暗号 输入框内容} param 
   */
  _InputAn:function(param){
    this.AnHaoText = param.detail.value; //暗号输入内容
  },
  /**
   * 跳转的home页面tabBar页面
   */
  gohome:function(e){
    wx.switchTab({
      url: '/pages/home/home'
    })
  },
  //跳转到发布页面
  _GoToPushAward:function(e){
    wx.switchTab({
      url: '/pages/pushlotto/pushlotto'
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})